package entity

import (
	"errors"
	"fmt"

	//	"log"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
	//log "github.com/cihub/seelog"
)

//UserNewOrgan 员工调动
type UserNewOrgan struct {
	ID            int          `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	EMID          int          `gorm:"column:EMID"`
	User          User         `gorm:"ForeignKey:EMID"`
	NEWEMID       *int         `gorm:"column:NEWEMID"`
	NewUser       User         `gorm:"ForeignKey:NEWEMID"`
	NEWEMID2      *int         `gorm:"column:NEWEMID2"`
	NewUser2      User         `gorm:"ForeignKey:NEWEMID2"`
	ZB1           float64      `gorm:"column:ZB1"`
	ZB2           float64      `gorm:"column:ZB2"`
	OUTID         int          `gorm:"column:OUTID"`
	OOrganization Organization `gorm:"ForeignKey:OUTID"`
	NUTID         int          `gorm:"column:NUTID"`
	NOrganization Organization `gorm:"ForeignKey:NUTID"`
	YJBD          string       `gorm:"column:YJBD"`
	YJBDCaption   string       `gorm:"-"`
	DDDT          time.Time    `gorm:"column:DDDT"`
	JZDT          time.Time    `gorm:"column:JZDT"`
	STEP          int          `gorm:"column:STEP"`
	STEPCaption   string       `gorm:"-"`
	DGCKISDR      int          `gorm:"column:DGCKISDR"` //对公存款是否导入
	DGDKISDR      int          `gorm:"column:DGDKISDR"` //对公贷款是否导入
	DSCKISDR      int          `gorm:"column:DSCKISDR"` //个人存款是否导入
	DSDKISDR      int          `gorm:"column:DSDKISDR"` //个人贷款是否导入
	ISCL          int          `gorm:"column:ISCL"`
	ISCLCaption   string       `gorm:"-"`
}

//TableName 员工调动表
func (UserNewOrgan) TableName() string {
	return "T_SysUser_YGDD"
}

//BeforeCreate 插入数据前
func (u *UserNewOrgan) BeforeCreate(tx *gorm.DB) (err error) {
	if u.EMID == 0 {
		return errors.New("默认员工不允许调动部门！")
	}
	var date time.Time
	var count int
	row := tx.Raw(`SELECT "DDDT",COUNT(*) count FROM "T_SysUser" A INNER JOIN "T_SysUser_YGDD" B ON A."ID"=B."EMID" WHERE A."ID"=? AND "DDDT">=? group by "DDDT" order by "DDDT" desc limit 1`, u.EMID, u.DDDT.Format("20060102")).Row()
	if err != nil {
		return
	}
	row.Scan(&date, &count)
	if count > 0 {
		return fmt.Errorf("员工调动日期必须在上次调动日期%s之后", date.Format("20060102"))
	}
	sqlstr := `SELECT "OrganID" FROM "T_SysUser" WHERE "ID"=?`
	row = tx.Raw(sqlstr, u.EMID).Row()
	row.Scan(&u.OUTID)
	if u.OUTID == u.NUTID {
		return errors.New("调出部门和调入部门不能相同！")
	}
	u.JZDT, err = time.Parse("20060102", "20991231")
	if err != nil {
		return err
	}
	jzdt := u.DDDT.AddDate(0, 0, -1)
	sqlstr = `update "T_SysUser_YGDD" set "JZDT"=? where "EMID"=? and "JZDT"='20991231'`
	tx.Exec(sqlstr, jzdt.Format("20060102"), u.EMID)
	return
}

//AfterFind 查询数据后
func (u *UserNewOrgan) AfterFind(tx *gorm.DB) (err error) {
	if u.YJBD == "1" {
		u.YJBDCaption = "业绩全部带走(含历史)"
	} else if u.YJBD == "2" {
		u.YJBDCaption = "业绩全部留给其他员工"
	} else if u.YJBD == "4" {
		u.YJBDCaption = "业绩部分带走"
	}
	if u.ISCL == 1 {
		u.ISCLCaption = "已处理"
	} else {
		u.ISCLCaption = "未处理"
	}
	return
}

//UserNewOrganEF 返回结构体
func UserNewOrganEF() base.EntityFunc {
	return func() interface{} {
		entity := UserNewOrgan{}
		return &entity
	}
}

//UserNewOrganESF 返回结构体数组
func UserNewOrganESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []UserNewOrgan
		return &entities
	}
}

func init() {
	relationship := []string{"NOrganization", "User", "OOrganization", "NewUser", "NewUser2"}
	base.DaoPoolObj.Put("UserNewOrgan", base.NewDefaultDao(UserNewOrganEF(), UserNewOrganESF(), relationship, "my"))
}
